/*
 * Copyright @ 2024 weili23
 * com.sky.service.impl 15:55
 * All right reserved.
 */

package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.service.ShoppingCardService;
import com.sky.mapper.ShoppingCardMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @desc:
 * @author: weili23
 * @createTime: 2024/11/7 15:55
 * @version: v1.0
 */
@Service
public class ShoppingCardServiceImpl extends ServiceImpl<ShoppingCardMapper, ShoppingCart> implements ShoppingCardService {

    /**
     * @desc: 添加购物车
     * @author: weili23
     * @Date: 2024/11/7 16:03
     * @version: 1.0
     */
    @Override
    public void addShoppingCard(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //获取用户
        Long currentId = BaseContext.getCurrentId();
        //1.先判断购物车有没有重复商品
        ShoppingCart searcgshoppingCart = this.lambdaQuery().eq(ShoppingCart::getUserId,currentId)
                .eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(StringUtils.isNotEmpty(shoppingCartDTO.getDishFlavor()), ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                .one();
        //2.有重复商品  数量加1
        if (searcgshoppingCart != null) {
            searcgshoppingCart.setNumber(searcgshoppingCart.getNumber() + 1);
            BeanUtils.copyProperties(searcgshoppingCart,shoppingCart);
            this.updateById(shoppingCart);
            return;
        }
        //3.没有重复商品
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(currentId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //判断是菜品还是套餐
        if(shoppingCartDTO.getDishId() !=null){
            //添加的是菜品信息
            Dish dish = Db.lambdaQuery(Dish.class).eq(Dish::getId, shoppingCartDTO.getDishId()).one();
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setName(dish.getName());
        }else{
            Setmeal setmeal = Db.lambdaQuery(Setmeal.class).eq(Setmeal::getId, shoppingCartDTO.getSetmealId()).one();
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setName(setmeal.getName());
        }
        //添加购物车
        this.save(shoppingCart);
    }

    @Override
    public List<ShoppingCart> getShoppingCardList() {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //根据用户id去查询购物车
        List<ShoppingCart> list = this.lambdaQuery().eq(ShoppingCart::getUserId, currentId).list();
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //根据用户id去删除名下的购物车
        QueryWrapper<ShoppingCart> qw = new QueryWrapper<>();
        qw.eq(ShoppingCart.USER_ID,currentId);
        this.remove(qw);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart card = new ShoppingCart();
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        //1.先判断购物车有没有重复商品，有的话就数量-1
        ShoppingCart one = this.lambdaQuery().eq(ShoppingCart::getUserId, currentId)
                .eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                .eq(StringUtils.isNotEmpty(shoppingCartDTO.getDishFlavor()), ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                .one();
        if(one !=null && one.getNumber()>1 ){
            BeanUtils.copyProperties(one,card);
            //数量-1 更新数据
            card.setNumber(one.getNumber() -1);
            updateById(card);
        }else{
            //直接删除数据
            this.removeById(one);
        }


    }
}
